﻿using System;
using System.Management.Automation;
using BGShell.PowerShell.Controls;

namespace BGShell.PowerShell.LanguageService
{
    partial class PSLanguageService
    {
        private struct ViewportInfo
        {
            private int _start;
            private int _end;

            private int _lineStart;
            private int _lineEnd;

            public bool Contains(PSToken token)
            {
                return TokenInRange(token, _start, _end);
            }

            public bool CurrentLineContains(PSToken token)
            {
                return TokenInRange(token, _lineStart, _lineEnd);
            }

            public static ViewportInfo Get(ConsoleControl control, ConsoleControl.InputReader reader)
            {
                return new ViewportInfo()
                {
                    _start = reader.GetFromRealCharIndex(control.FirstVisibleCharIndex),
                    _end = reader.GetFromRealCharIndex(control.LastVisibleCharIndex),

                    _lineStart = reader.GetFromRealCharIndex(reader.CurrentLine.FirstCharIndex),
                    _lineEnd = reader.GetFromRealCharIndex(reader.CurrentLine.LastCharIndex),
                };
            }

            private static bool TokenInRange(PSToken token, int start, int end)
            {
                var tokenStart = token.Start;

                return (tokenStart >= start) && (tokenStart <= end);
            }
        }
    }
}
